<template>
  <div style="width:200px;height:50px;margin: 0 auto;font-size: 30px;margin-top:40px;margin-bottom:20px" >身份核验</div>
<div style="border:1px solid #000;width:896px;margin: 0 auto" >
  <BasicForm @register="registerForm1" style="margin-top:20px">
    <template #nameslot="{ model, field }">
      <div style="display:flex">
      <a-input v-model:value="model[field]" type="Input"    readOnly/>
        &nbsp;&nbsp;&nbsp;
      <a-button @click="ReadCard">读身份证</a-button>
      </div>
    </template>
    <template #paperNumberslot="{ model, field }">
      <div style="display:flex">
        <a-input v-model:value="model[field]" type="Input"    readOnly/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </div>
    </template>

  </BasicForm>
</div>
  <div style="margin:0 auto;width:896px;margin-top:40px;margin-bottom:20px;font-size: 20px;">预约/登记信息</div>
   <div style="border:1px solid #000;width:896px;margin: 0 auto;margin-top:20px;margin-bottom:50px" >
    <BasicForm @register="registerForm"  style="margin-top:20px"/>
   </div>
  <div style="display:flex;width:200px;margin: 0 auto;margin-bottom:50px">
    <a-button type="primary" v-on:click="allowin"> 放行 </a-button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a-button type="primary" danger  v-on:click="refusein"> 拒绝 </a-button>
  </div>
</template>

<script lang="ts" setup>
  import {ref, computed, unref} from 'vue';
  import {BasicModal, useModalInner} from '/@/components/Modal';
  import {BasicForm, FormSchema, useForm} from '/@/components/Form/index';
  import {formSchema} from '../appointment/SsVisitAppointment.data';
  import {list, saveOrUpdate} from './SsVisitWhiteblack.api';
  import {list1,listToday} from '../appointment/SsVisitAppointment.api';
  import {list as listp} from '/@/views/baseinfo/person/SsPerson.api';

  import  "./language.js";
  //  import    "./Client_Card.js";
  import    "./Client.js";
  import    "./app.js";
  import   "@/views/visitor/bw/test";
  import {connect, ReadCard, settbact,setformact} from "/@/views/visitor/bw/app";
  import {setcmessage} from "/@/views/visitor/bw/Client";
  import {BasicColumn} from "@/components/Table";
  import {checkVisitor} from "/@/views/visitor/record/SsVisitRecord.api";

  import {UploadTypeEnum} from "@/components/Form/src/jeecg/components/JUpload";
  import {onMountedOrActivated} from "@/hooks/core/onMountedOrActivated";
  import {useMessage} from "@/hooks/web/useMessage";
  // Emits声明
  const emit = defineEmits(['register','success']);
  const isUpdate = ref(true);
  const { createMessage, createErrorModal,createWarningModal,createInfoModal } = useMessage();
  //表单配置
  const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
    labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: {span: 12},
    wrapperCol: {
      xxl: 12,
    },
  });

  const formSchema1: FormSchema[] = [
    {
      label: '姓名',
      field: 'name',
      component: 'Input',
      colProps: {span: 17},
      slot:"nameslot",
    },
    {
      label: '证件号码',
      field: 'paperNumber',
      component: 'Input',
      colProps: {span: 17},
      slot:"paperNumberslot",
    },
    {
      label: '有效日期',
      field: 'expireDate',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        placeholder:"有效日期",
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      label: '居住地址',
      field: 'address',
      component: 'Input',
      componentProps:{
        placeholder:"居住地址",
        readOnly:true,
      }
    },
    {
      label: '头像',
      field: 'photo',
      component: 'JUpload',
      helpMessage: '最多上传1张图片',
      componentProps: {
        fileType: UploadTypeEnum.image,
        bizPath: 'bwphoto',
        maxCount: 1,
      },
    },
    {
      label: '权限类型',
      field: 'type',
      component: 'JDictSelectTag',
      componentProps:{
        dictCode:"visit_bw_type",
        stringToNumber:true,
      }
    },
    {
      label: '',
      field: 'id',
      component: 'Input',
      show: false
    },

  ]
  const [registerForm1, {setProps:setProps1,resetFields:resetFields1, setFieldsValue:setFieldsValue1, validate:validate1}] = useForm({
    labelWidth: 150,
    schemas: formSchema1,
    showActionButtonGroup: false,
    baseColProps: {span: 12},
    wrapperCol: {
      xxl: 12,
    },
  });

  //表单赋值
  const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    }
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter })
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({confirmLoading: true});
      //提交表单
      await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({confirmLoading: false});
    }
  }
  function formaData(timer) {
    const year = timer.getFullYear()
    const month = timer.getMonth() + 1 // 由于月份从0开始，因此需加1
    const day = timer.getDate()
    const hour = 0;
    const minute = 0;
    const second = 0;
    return `${pad(year, 4)}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
  }
  function pad(timeEl, total = 2, str = '0') {
    return timeEl.toString().padStart(total, str)
  }
  const addOneDay = (strDate) => {
    let dateFormat = new Date(strDate);
    dateFormat =dateFormat.setDate(dateFormat.getDate()+1);
    dateFormat  = new Date(dateFormat );
    let y = dateFormat.getFullYear()
    let m = (dateFormat.getMonth() + 1).toString().padStart(2, '0')
    let d = dateFormat.getDate().toString().padStart(2, '0')
    return dateFormat
  }
  async function setform(info){
    console.log(info);
    let params={paperNumber:info.idnum,paperType:0,queryFrom:0};
    let res =await list(params)
    console.log(res);
    if(res.records?.length!=0){
      const bw  =  res.records[0];
      res.records[0].address = info.address;
      let te = info.expdateend
      let expd = te.substr(0,4)+"-"+te.substr(4,2)+"-"+te.substr(6,2);
      console.log(expd)
      res.records[0].expireDate=expd;
      setFieldsValue1({...bw})
    }

   // if(bw.apmId && bw.type==3){
      let params1 = {wbpaperType:0,wbpaperNumber:info.idnum,willVisitTime_begin:formaData(new Date()),willVisitTime_end: formaData(addOneDay(new Date()))};
      let res1 =await list1(params1);
      console.log(res1);
      console.log(info)
      if(res1.records?.length>0) {

        //res1.records[0].expireDate=info.expdateend;
        setFieldsValue(res1.records[0]);
        let temp = res1.records[0].meetPersonId;
        console.log(temp)
        changeEvent(temp);
      }

   // }


  }
  function changeEvent(e){
    if(!e){
      setFieldsValue({personName:"",personCode:"",personDept:"",personType:""})
      return;
    }
    let params = {id:e};
    console.log(params);
    let res =  listp(params);
    res.then((data)=>{
      console.log(data);
      if(data.records?.length>0){
        setFieldsValue({personName:data.records[0].name,personCode:data.records[0].code,personDept:data.records[0].departmentId_dictText,personType:data.records[0].personType})
      }

    })
  }
  async function allowin(){
      console.log("allow in");
    let values = await validate();
    let values1 = await validate1();
    let params = {};

    if(values.id){
      params.id = values.id;
      params.type = "ap";
    }
    if(values1.id){
      params.id = values1.id;
      params.type="bw";
    }
    let res = await checkVisitor(params);

  }
  function refusein(){
    console.log("refuse in");
  }

  onMountedOrActivated(()=>{
    // getForm().setFieldsValue({paperNumber:"jfkdfjkd"});
    setformact(setform);
    setcmessage(createMessage)
    connect();
  })
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number){
    width: 100%
  }

  :deep(.ant-calendar-picker){
    width: 100%
  }
</style>
